<!-- 模板 -->
<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 登录表单区域 Element ui -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <!-- 第三方图标库此处是 阿里图标库 -->
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="restLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<!-- 行为 -->
<script>
export default {
  data() {
    return {
      //登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    restLoginForm() {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      //一个形参 可以省略小括号((validRes)=>{
      // this.$refs.loginFormRef.validate(validRes => 由于使用await方法,所以要在这里加上async
      this.$refs.loginFormRef.validate(async validRes => {
        if (!validRes) {
          console.log('验证失败,请重试')
          return
        }
        //const result = this.$http.post('login',this.loginForm)
        //返回值是promise 可以用await async 方法简化返回值,await
        //在打印出来就不是promise对象,而是data对象
        //const result = await this.$http.post("login", this.loginForm);
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) {
          this.$message.error('登陆失败')
          return
        }
        this.$message({
          showClose: true,
          message: '你已经成功登录',
          type: 'success',
          duration: 0,
        })
        //1登录成功保存token ,存到客户端sessionStorage中
        //1.1 除了登录之外的API接口,必须在登录后才能访问
        //1.2 token只有在当前打开期间有效,所以存在sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        //成功后跳转到home页面,通过router路由
        this.$router.push('/home')
      })
    }
  }
}
</script>

<!-- 样式 -->
<!-- scoped作用: 样式只对当前组件有效 -->
<style lang="less" scoped>
.el-message__content {
  color: #fff;
  background-color: rgb(226, 8, 8);
}
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;
    img {
      border-radius: 50%;
      height: 100%;
      width: 100%;
      background-color: #eee;
    }
  }
  .btns {
    //float: right;
    display: flex;
    justify-content: flex-end;
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
